package com.zhen;

import com.zhen.domain.Clazz;
import com.zhen.domain.Student;
import com.zhen.mapper.ClazzMapper;
import com.zhen.mapper.StudentMapper;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

public class OneToManyTest {

    public static void main(String[] args) throws IOException {
        InputStream inputStream = Resources.getResourceAsStream("mybatis-config.xml");
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        SqlSession sqlSession = sqlSessionFactory.openSession();
        OneToManyTest oneToManyTest = new OneToManyTest();
//        oneToManyTest.testSelectClazzById(sqlSession);
        oneToManyTest.testSelectStudentById(sqlSession);
        sqlSession.commit();
        sqlSession.close();
    }

    public void testSelectClazzById(SqlSession session){
        ClazzMapper clazzMapper = session.getMapper(ClazzMapper.class);
        Clazz clazz = clazzMapper.selectClazzById(1);
        System.out.println(clazz.getId() + " " + clazz.getCode() + " " + clazz.getName());

        List<Student> studentList = clazz.getStudents();
        for(Student student : studentList){
            System.out.println(student);
        }
    }

    public void testSelectStudentById(SqlSession session){
        StudentMapper studentMapper = session.getMapper(StudentMapper.class);
        Student student = studentMapper.selectStudentById(1);
        System.out.println(student);
        System.out.println(student.getClazz());
    }
}
